package com.wan37.logic.guild.dao.impl;

import com.wan37.logic.guild.dao.GuildUserDao;
import com.wan37.logic.guild.domain.entity.GuildUserDO;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-03 17:29
 **/
@Repository
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class GuildUserDaoImpl implements GuildUserDao {
    @Autowired
    HibernateTemplate hibernateTemplate;

    @Override
    public boolean add(GuildUserDO guildUserDO) {
        hibernateTemplate.save(guildUserDO);
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        hibernateTemplate.delete(id);
        return false;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(GuildUserDO guildUserDO) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("update GuildUserDO set job = ? where guildId=? and userId=?");
        query.setParameter(0, guildUserDO.getJob());
        query.setParameter(1, guildUserDO.getGuildId());
        query.setParameter(2, guildUserDO.getUserId());
        query.executeUpdate();
        return false;
    }

    @Override
    public List<GuildUserDO> getAll() {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GuildUserDO> getGuildUserDOByGuildId(String guildId) {
        return (List<GuildUserDO>) hibernateTemplate.find("from GuildUserDO where guildId = ?", guildId);
    }

    @Override
    public List<GuildUserDO> getGuildUserDOByGuildIdAndUserId(String guildId, String userId) {
        return (List<GuildUserDO>) hibernateTemplate.find("from GuildUserDO where guildId = ? and userId = ?", guildId, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByGuildIdAndUserId(String guildId, String userId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from GuildUserDO where guildId=? and userId=?");
        query.setParameter(0, guildId);
        query.setParameter(1, userId);
        query.executeUpdate();
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByGuildId(String guildId) {
        Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
        Query query = session.createQuery("delete from GuildUserDO where guildId=?");
        query.setParameter(0, guildId);
        query.executeUpdate();
        return false;
    }
}
